package com.xry.result.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xry.common.bean.TicketResult;
import com.xry.common.service.TicketResultService;
import com.xry.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ticketResult")
public class TicketResultController {
    @Autowired
    TicketResultService ticketResultService;
    @GetMapping("{id}")
    public Result getTicketResultById(@PathVariable("id") Integer id) {
        try{
            Object obj=ticketResultService.getTicketResultById(id);
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }

    @PostMapping("/addTicketResult")
    public Result addTicketResult(@RequestBody TicketResult ticketResult) {
        try{
            ticketResultService.addTicketResult(ticketResult);
            ticketResult = ticketResultService.getTicketResultById(ticketResult.getId());
            return Result.success("新增成功", ticketResult);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @PutMapping("/updateTicketResultById")
    public Result updateTicketResultById(@RequestBody TicketResult ticketResult) {
        try{
            boolean flag=ticketResultService.updateTicketResultById(ticketResult);
            if(flag){
                return Result.success("修改成功", ticketResult);
            }else{
                return Result.fail(500,"修改失败");
            }
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @DeleteMapping("{id}")
    public Result deleteTicketResultById(@PathVariable("id") Integer id) {
        try{
            boolean flag=ticketResultService.deleteTicketResultById(id);
            if(flag){
                return Result.success("删除成功");
            }else{
                return Result.fail(500,"删除失败");
            }
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @GetMapping("/getAll")
    public Result getPage() {
        try{
            Object obj=ticketResultService.getAll();
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @GetMapping("/getPage")
    public Result getPage(@RequestParam(defaultValue = "1") Integer current,
                          @RequestParam(defaultValue = "10") Integer size,
                          TicketResult ticketResult) {
        try{
            // 创建分页对象
            IPage<TicketResult> page = new Page<>(current, size);
            Object obj = ticketResultService.getPage(ticketResult, page);
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }


}
